package com.app.ui.view.list.head;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.LinearLayout;

import com.app.utiles.other.DLog;


public class BaseRefreshLayout extends LinearLayout implements ValueAnimator.AnimatorUpdateListener,
        Animator.AnimatorListener, ListRefreshCustom.OnRefreshListener {
    //true 刷新中
    protected boolean isRefresh;
    protected HandlerRefresh handlerRefresh;

    public BaseRefreshLayout(Context context) {
        super(context);
        init();
    }

    public BaseRefreshLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BaseRefreshLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        handlerRefresh = new HandlerRefresh();
    }

    public int getVisiableHeight() {
        return 0;
    }

    public boolean isRefresh() {
        return isRefresh;
    }

    public ListRefreshCustom.OnRefreshListener getRenovationListener() {
        return this;
    }

    //=====================================箭头旋转动画==============================================
    private RotateAnimation rotateAnimationUp;
    private RotateAnimation rotateAnimationD;

    //箭头旋转动画
    public void startRotateAnimation(View view, float start, float end, int duration, boolean isUp) {
        view.clearAnimation();
        if (isUp && rotateAnimationUp == null) {
            rotateAnimationUp = new RotateAnimation(start, end,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimationUp.setDuration(duration);
            rotateAnimationUp.setFillAfter(true);
        }
        if (!isUp && rotateAnimationD == null) {
            rotateAnimationD = new RotateAnimation(start, end,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimationD.setDuration(duration);
            rotateAnimationD.setFillAfter(true);
        }
        RotateAnimation rotateAnimation = isUp ? rotateAnimationUp : rotateAnimationD;
        view.startAnimation(rotateAnimation);
        rotateAnimation.start();
    }

    //========================回滚动画==========================
    private ValueAnimator valueAnimatorSmall;

    public void startAnimatorView(int length, int time) {
        DLog.e("动画回滚距离", length);
        if (length == 0) {
            return;
        }
        if (valueAnimatorSmall == null) {
            valueAnimatorSmall = ValueAnimator.ofInt();
            valueAnimatorSmall.addUpdateListener(this);
            valueAnimatorSmall.addListener(this);
        }
        valueAnimatorSmall.cancel();
        valueAnimatorSmall.setFloatValues(0, length);
        valueAnimatorSmall.setDuration(time);
        valueAnimatorSmall.start();
    }


    //=====================================动画监听==============================================

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {

    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {

    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    //=====================================下拉刷新监听==============================================
    @Override
    public void onTouchEvent(float f) {

    }

    @Override
    public boolean onTouchEventStop() {
        return false;
    }

    @Override
    public void setRefreshComplete() {
    }

    //==============================
    protected void handleMessage(Message msg) {

    }

    class HandlerRefresh extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            BaseRefreshLayout.this.handleMessage(msg);
        }
    }
}
